namespace NFTRedit
{
    using System;
    using System.IO;

    public class FINFChunk
    {
        private static readonly uint MAGIC = 0x46494e46;
        private uint mFirstCmapOffset;
        private uint mFirstCwdhOffset;
        private uint mSize;
        private uint mUnknown1;
        private uint mUnknown2;
        private uint mUnknown3;

        public FINFChunk(BinaryReader reader)
        {
            this.Load(reader);
        }

        private void Load(BinaryReader reader)
        {
            if (reader.ReadUInt32() != MAGIC)
            {
                throw new FormatException("Chunk at offset " + string.Format("0x{0:x4}", reader.BaseStream.Position - 4L) + " is not FINF format.");
            }
            this.mSize = reader.ReadUInt32();
            this.mUnknown1 = reader.ReadUInt32();
            this.mUnknown2 = reader.ReadUInt32();
            this.mUnknown3 = reader.ReadUInt32();
            this.mFirstCwdhOffset = reader.ReadUInt32();
            this.mFirstCmapOffset = reader.ReadUInt32();
        }

        public void Save(BinaryWriter writer)
        {
            writer.Write(MAGIC);
            writer.Write(this.mSize);
            writer.Write(this.mUnknown1);
            writer.Write(this.mUnknown2);
            writer.Write(this.mUnknown3);
            writer.Write(this.mFirstCwdhOffset);
            writer.Write(this.mFirstCmapOffset);
        }

        public int FirstCmapOffset
        {
            get
            {
                return (((int) this.mFirstCmapOffset) - 8);
            }
            set
            {
                this.mFirstCmapOffset = (uint) (value + 8);
            }
        }

        public int FirstCwdhOffset
        {
            get
            {
                return (((int) this.mFirstCwdhOffset) - 8);
            }
            set
            {
                this.mFirstCwdhOffset = (uint) (value + 8);
            }
        }

        public int Length
        {
            get
            {
                return (int) this.mSize;
            }
        }
    }
}

